//
//  FindAndDel.m
//  618
//
//  Created by Ling on 13-7-9.
//
//

#import "BallSprite.h"

#import "FindAndDel.h"
@interface FindAndDel()

+(void)findBatchBallDiraction:(int)x xStep:(int)x1 xContStep:(int)x2 yCordination:(int)y yStep:(int)y1 yContStep:(int)y2 currentAdded:(NSMutableArray*)arrAdded;
@end

@implementation FindAndDel



//find the match five+ ball and mark as del_abled


+(NSMutableArray*)findMatchBall:(BallSprite *)currentBall currentAdded:(NSMutableArray*)arrAdded
{
    
    int type = currentBall.type;
    NSMutableArray *temparray = [[[NSMutableArray alloc] init] autorelease];
    for (int j=0; j< arrAdded.count; j++) {
        BallSprite *sp = (BallSprite *)[arrAdded objectAtIndex:j];
        if (type == sp.type) {
            [temparray  addObject:sp];
        }
        
    }
    Log_Debug(@"竖方向");
    [self findBatchBallDiraction:currentBall.x xStep:0 xContStep:0 yCordination:currentBall.y yStep:1 yContStep:-1 currentAdded:[temparray mutableCopy]];
    Log_Debug(@"从左到右方向");
    [self findBatchBallDiraction:currentBall.x xStep:1 xContStep:-1 yCordination:currentBall.y yStep:1 yContStep:-1 currentAdded:[temparray mutableCopy]];
    Log_Debug(@"横方向");
    [self findBatchBallDiraction:currentBall.x xStep:1 xContStep:-1 yCordination:currentBall.y yStep:0 yContStep:0 currentAdded:[temparray mutableCopy]];
    Log_Debug(@"从右到右方向");
    [self findBatchBallDiraction:currentBall.x xStep:-1 xContStep:1 yCordination:currentBall.y yStep:1 yContStep:-1 currentAdded:[temparray mutableCopy]];
    return temparray;
    
    
}
//every time find the (x+x1,y+y1) and (x+x2,y+y2)
+(void)findBatchBallDiraction:(int)x xStep:(int)x1 xContStep:(int)x2 yCordination:(int)y yStep:(int)y1 yContStep:(int)y2 currentAdded:(NSMutableArray*)arrAdded
{
    int tempX = x;
    int tempY = y;
    int iCount = arrAdded.count;
    NSMutableArray *temparray = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<iCount;++i) {
        BallSprite *sp = (BallSprite *)[arrAdded objectAtIndex:i];
        if (sp.x == tempX+x1 && sp.y==tempY+y1) {
            [temparray addObject:sp];
            [arrAdded removeObjectAtIndex:i];
            iCount = arrAdded.count;
            tempX = sp.x;
            tempY = sp.y;
            i =-1;
        }
        
    }
    tempX = x;
    tempY = y;
    iCount = arrAdded.count;
    for (int i=0; i<iCount;++i) {
        BallSprite *sp = (BallSprite *)[arrAdded objectAtIndex:i];
        if (sp.x == tempX+x2 && sp.y==tempY+y2) {
            [temparray addObject:sp];
            [arrAdded removeObjectAtIndex:i];
            iCount = arrAdded.count;
            tempX = sp.x;
            tempY = sp.y;
            i =-1;
        }
        
    }
    if (temparray.count<4) {
        Log_Debug(@" [0] 个球.");
        return;
    }
    
    Log_Debug(@"共有 [%d] 个球需要删除.",temparray.count);
    for (int i=0; i<temparray.count; ++i) {
        BallSprite *sp = (BallSprite *)[temparray objectAtIndex:i];
        sp.del = YES;
    }
    
    
}

@end
